import 'package:markdown/markdown.dart' as md;

/// 块级LaTeX公式语法解析器
/// 用于解析形如 $$...$$的LaTeX公式
class MathSyntax extends md.BlockSyntax {
  static final _pattern = RegExp(r'^\s*\$\$(.*?)\$\$\s*$', multiLine: true, dotAll: true);

  MathSyntax();

  @override
  RegExp get pattern => _pattern;

  @override
  md.Node parse(md.BlockParser parser) {
    final match = pattern.firstMatch(parser.current.content);
    final latex = match?.group(1)?.trim() ?? '';
    parser.advance();
    return md.Element('math', [md.Text(latex)]);
  }
}

/// 行内LaTeX公式语法解析器
/// 用于解析形如 $...$的LaTeX公式
class InlineMathSyntax extends md.InlineSyntax {
  InlineMathSyntax() : super(r'\$(.*?)\$');

  @override
  bool onMatch(md.InlineParser parser, Match match) {
    final latex = match.group(1)?.trim() ?? '';
    parser.addNode(md.Element('inline_math', [md.Text(latex)]));
    return true;
  }
}